<template>
<!-- 结构 -->
  <el-container class="my-layout">
    <el-aside width="200px">
      <img src="@/assets/images/logo.png" alt="" width="100%" style="text-align: center;">
      <!-- background-color="#023"  设置背景颜色为#023
      default-active="$route.path"  设置默认激活的菜单项为当前路由路径
      text-color='#fff' 设置菜单文本颜色为白色
      style="border: none;"  移除边框样式
      active-text-color="#409EFF"  设置激活菜单项的文本颜色为#409EFF
      router 启用路由模式，菜单项的索引会被当作路径-->
      <el-menu
        background-color="#023"
        default-active="$route.path"
        text-color='#fff'
        style="border: none;"
        active-text-color="#409EFF"
        router
      >
        <el-menu-item index="/"><i class="el-icon-s-home"></i>数据概览</el-menu-item>
        <el-menu-item index="/article"><i class="el-icon-document"></i>内容管理</el-menu-item>
        <el-menu-item index="/publish"><i class="el-icon-s-promotion"></i>发布文章</el-menu-item>
        <el-menu-item index="/my"><i class="el-icon-s-custom"></i>个人中心</el-menu-item>
      </el-menu>
    </el-aside>
    <el-container>
      <!-- 头部 -->
       <el-header>
          <span>{{ user.mobile || user.name }}</span>
          <el-link @click="onLogout()" :underline="false" icon="el-icon-unlock">退出</el-link>
       </el-header>
       <!-- 主体 -->
        <el-main>
          <!-- 路由出口 -->
           <router-view></router-view>
        </el-main>
    </el-container>
  </el-container>
</template>

<script>
import auth from '@/utils/token'
// 逻辑
export default {
  name: 'LayoutPage',
  data () {
    return {
      user: {}
    }
  },
  async created () {
    // 在拦截器中自动拼接Token
    const res = await this.$http.get('/v1_0/user/profile')
    // console.log(res)
    this.user = res.data.data
  },
  methods: {
    onLogout () {
      this.$confirm('此操作将退出登录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // 清空Token
        auth.removeToken()
        this.$router.push('/login')
        this.$message.success('退出成功')
      }).catch(() => {
      })
    }
  }
}
</script>

<style scoped>
/* 样式 */
.el-aside{
  background-color: #023
}
.my-layout{
  height: 100vh;
}
.el-header{
  display: flex;
  justify-content: end;
  align-items: center;
  gap: 10px;
}
</style>
